%load_ext pretty_jupyter

Conda

miniconda is a light version of anaconda, it contains only conda, python and other necessary tools. miniforge is similar to minicoda but conda-forge is the only channel by default. miniconda has package installer while miniforge has only command installer.

Use conda-forge channel instead of defaults channel when installing packages and use pip only when conda doesn't have certain packages.

记住一句话:类是模板,而实例则是根据类创建的对象。我初学时对类的理解是从类的字面上,可以片面的认为它是一个种类,它是相似特征的抽像,也就是相似的东西,可以把相似特征的事务抽象成一个类。(事务可以是具体的物体或行为)以圆为例,圆是具有圆周率(pi)和半径(r)两个相似特征的属性。根据相似特征抽象出圆类,每个圆的半径可以不同,那么半径可以作为圆的实例属性;而每个圆的圆周率pi是相同的,那么圆周率pi就可以作为类属性,这样就定义出了一个圆类。而我们要知道圆的面积,周长等可以通过类方法计算出来。

作者:木头人 链接:https://zhuanlan.zhihu.com/p/30024792 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Install conda

First, go to miniconda installer, download macos.pkg and install it.

Second, check if conda is configured using conda --version. If not, it needs to be configured in file .zshrc (a place for personal configurations):

  • Open this file in terminal using vim ~/.zshrc,
  • Type i to open edit mode (--INSERT-- will appear at bottom),
  • Write export PATH=/Users/chengfengliu/Softwares/miniconda3/bin:$PATH in a new line,
  • Press esc to close edit mode,
  • Move the cursor to end and type : (: will appear at bottom), type wq (add ! if there is readonly problem) after : and press enter to close .zshrc,
  • Make the edited configurations effective using source ~/.zshrc,
  • Check again using conda --version.

Third, set conda-forge as the only channel:

  • Check current channels with priorities using conda config --get channels,
  • Add conda-forge channel with the highest priority using conda config --add channels conda-forge,
  • Set channel priority strict using conda config --set channel_priority strict,
  • Remove defaults channel using conda config --remove channels defaults,
  • Check again using conda config --get channels,
  • In base env, update all packages from defaults channel to conda-forge channel using conda update --all,
  • Check by conda list.

If xcrun error occurs, use xcode-select --install.

Conda commands

conda --version
conda list

conda search --full-name pkg_precise
conda search pkg_pattern

conda install pkg=version
conda uninstall pkg

conda update conda
conda update pkg=version
conda update --all

conda create --name env python=version
conda create --name env1 --clone env2
conda activate env
conda deactivate
conda env list
conda env remove --name env

conda config --get channels
conda config --add channels channel
conda config --set channel_priority strict
conda config --remove channels channel

Pip

Install pip

Installing python using conda when creating an env comes with pip.

Mac uses zsh in terminal which causes no matches found issue with spuare brackets, solution follows:

  • Open .zshrc file in terminal using vim ~/.zshrc,
  • Type i to open edit mode (--INSERT-- will appear at bottom),
  • Write setopt no_nomatch in a new line,
  • Press esc to close edit mode,
  • Move the cursor to end and type : (: will appear at bottom), type wq (add ! if there is readonly problem) after : and press enter to close .zshrc,
  • Make the edited configurations effective using source ~/.zshrc.

Pip commands

pip --version
pip list
pip list --outdated

pip check pkg
pip check

pip install pkg==version
pip install --upgrade pip
pip install --upgrade pkg==version
pip uninstall pkg

pip freeze
pip freeze > requirements.txt
pip install -r requirements.txt

Environments

Using Jupyter extension in VSCode doesn't require Jupyter Lab or Notebook installed because they are user interfaces just like VSCode, but the following packages are needed:

  • ipykernel converts conda environments into kernels
  • nbconvert with pretty-jupyter exports Jupyter notebooks into htmls

Create data environment in base environment:

conda create --name data ipykernel \
    python python-docx pandas openpyxl \
    scikit-learn py-xgboost lightgbm catboost optuna \
    langchain openai transformers \
    plotly nbconvert

pip install pretty-jupyter

To activate data environment and enter into Projects directory when launching terminal:

  • Open .zshrc file in terminal using vim ~/.zshrc,
  • Type i to open edit mode (--INSERT-- will appear at bottom),
  • Write conda activate data and cd /Users/chengfengliu/Documents/Projects in separated new lines,
  • Press esc to close edit mode,
  • Move the cursor to end and type : (: will appear at bottom), type wq (add ! if there is readonly problem) after : and press enter to close .zshrc,
  • Make the edited configurations effective using source ~/.zshrc.

Plotly

import plotly.express as px

df = px.data.iris()

fig = px.scatter_matrix(df, 
                        dimensions=["sepal_width", 
                                    "sepal_length", 
                                    "petal_width", 
                                    "petal_length"], 
                        color="species")

fig.show(renderer='notebook')